package com.struts2.action;

import java.text.ParseException;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.struts2.model.Designer;
import com.struts2.service.RegService;

public class RegAction extends ActionSupport implements ModelDriven<Designer>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Integer NULL = null;
	
	private Designer designer = new Designer();
	

	public Designer getDesigner() {
		return designer;
	}

	public void setDesigner(Designer designer) {
		this.designer = designer;
	}
	
	public void validate() {
		System.out.println("validate start");
		if (StringUtils.isEmpty(designer.getName())) {
			addFieldError("name", "Name cannot be blank");
		}
		if (StringUtils.isEmpty(designer.getPassword())) {
			addFieldError("password", "Password cannot be blank");
		}
		if (StringUtils.isEmpty(designer.getEmail())) {
			addFieldError("email", "Email cannot be blank");
		}
		if (designer.getPhone_number() == NULL) {
			addFieldError("phone_number", "Phone number cannot be blank");
		}
		if (designer.getBank_account_number() == 0L) {
			addFieldError("bank_account_number", "Bank account number cannot be blank");
		}
		if (StringUtils.isEmpty(designer.getUsername())) {
			addFieldError("username", "User Name cannot be blank");
		}
    }
	
	public String Registration() throws ParseException {
		System.out.println("reg action called");
		RegService regService = new RegService();
		String result = regService.verifyReg(designer);
		if (StringUtils.containsAny(result, "u"))
			addActionError("User Name has been used");
		if (StringUtils.containsAny(result, "n"))
			addActionError("Name has been used");
		if (StringUtils.containsAny(result, "p"))
			addActionError("Phone number has been used");
		if (StringUtils.containsAny(result, "u") || StringUtils.containsAny(result, "n") || StringUtils.containsAny(result, "p")){
			return "input";}
		regService.insertDesigner(designer);
		return SUCCESS;
	}

	@Override
	public Designer getModel() {
		// TODO Auto-generated method stub
		return designer;
	}
}
